<template>
    <div class="login-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
            label-position="left">
            <div class="title-container">
                <img src="https://ancientapi.pro.aisisoft.com.cn/static/images/title.png" />
            </div>
            <el-form-item prop="account">
                <span class="svg-container">
                    <img src="@/assets/icons/user.svg" />
                </span>
                <el-input ref="account" v-model="loginForm.account" placeholder="账号" name="account" type="text" tabindex="1"
                    auto-complete="on" />
            </el-form-item>

            <el-form-item prop="password">
                <span class="svg-container">
                    <img src="@/assets/icons/password.svg" />
                </span>
                <el-input ref="password" v-model="loginForm.password" type="password" placeholder="密码" name="password"
                    tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
            </el-form-item>
            <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px; margin-top: 10px"
                size="big" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
import { login } from '@/api/login'
export default {
    data() {
        const validateUsername = (rule, value, callback) => {
            if (value.length < 1) {
                callback(new Error('请输入账号'))
            } else {
                callback()
            }
        }
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('请输入6位以上的密码'))
            } else {
                callback()
            }
        }
        return {
            loginForm: {
                account: '',
                password: '',
            },
            loginRules: {
                account: [{ required: true, trigger: 'blur', validator: validateUsername }],
                password: [{ required: true, trigger: 'blur', validator: validatePassword }]
            },
            loading: false,
        }
    },
    created() {
    },
    methods: {
        handleLogin() {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    login(this.loginForm).then(res => {
                        console.log(res);
                        this.loading = false
                        if (res.status == 1) {
                            localStorage.setItem('token', res.data)
                            this.$message({
                                message: res.info,
                                type: 'success'
                            });
                            this.$router.push('/')
                        } else {
                            this.$message({ type: 'error', message: res.info });
                        }
                    }).catch(e => {
                        console.log(e);
                    })
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
    }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-ui css */
.login-container {
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;


.login-container {
    height: 100vh;
    width: 100%;
    // background-color: $bg;
    position: relative;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 40px 40px 0;
        margin: 0 auto;
        margin-top: 15%;
        overflow: hidden;
        background: rgba(0, 0, 0, 0.75);

    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;

        .svg-icon {
            border: 1px solid;
        }
    }

    .title-container {
        position: relative;
        display: flex;
        justify-content: center;
        margin-bottom: 20px;

        img {
            width: 80%;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .thirdparty-button {
        position: absolute;
        right: 0;
        bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
        .thirdparty-button {
            display: none;
        }
    }
}

.login-container::before {
    content: "";
    background: url('https://ancientapi.pro.aisisoft.com.cn/static/images/bj.png') no-repeat;
    background-size: 100% 100%;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
}
</style>